package cc.yugu.yhdw.webserver.web.controller.security.hdw;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.domain.basic.User;
import cc.yugu.yhdw.common.domain.hdw.AgentHdwSetting;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.service.hdw.AgentHdwSettingService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/security/hdw/agent_hdw_setting")
public class AgentHdwSettingController extends SecurityController {

    @Autowired
    AgentHdwSettingService agentHdwSettingService;

    @RequestMapping(value = "page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(AgentHdwSetting agentHdwSetting, HttpServletRequest request) {
        if (getSessionUser(request).getType() == User.Type.AGENT.getValue() || getSessionUser(request).getType() == User.Type.AGENCY.getValue()) {
            agentHdwSetting.setId(getSessionUser(request).getAgentId());
        }
        return PageResult.successResult(agentHdwSettingService.findPage(agentHdwSetting));
    }

    @RequestMapping(value = "index.htm")
    public void index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_03_06.getValue());
    }

    @RequestMapping("update.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult update(AgentHdwSetting entity) {
        return agentHdwSettingService.update(entity);
    }

    @RequestMapping("view.htm")
    public void view(int id, Model model) {
        model.addAttribute("entity", agentHdwSettingService.find(id));
    }

    @RequestMapping("edit.htm")
    public void edit(int id, Model model) {
        model.addAttribute("entity", agentHdwSettingService.find(id));
    }
}
